using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Ultimate.DAL.CollectionClasses;
using Ultimate.DAL;
using Ultimate.DAL.HelperClasses;
using Ultimate.DAL.EntityClasses;
using Ultimate.DAL.FactoryClasses;
using SD.LLBLGen.Pro.ORMSupportClasses;

namespace CommunityServerWeb.RegistrationSystem.Controls
{
    public partial class SignUpButton : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //Check 1:
                //If the user is anonymous check if there are any leagues open for registration and 
                //if so, show the LoggedOutView
                //Check 2:
                //If the user is not anonymous check if there are any leagues open for registration that the
                //user has not entered and
                //if so, show the LoggedInView
                //Default:
                //If the above situations aren't met, don't display this control.

                if (CommunityServer.Users.GetUser().IsAnonymous)
                {
                    if (IsALeagueOpenForRegistration())
                    {
                        multiPass.SetActiveView(vwLoggedOut);
                    }
                    else
                    {
                        this.Visible = false;
                    }
                }
                else
                {
                    System.Web.Security.MembershipUser mu = System.Web.Security.Membership.GetUser();
                    Guid userID = (Guid)mu.ProviderUserKey;
                    if (IsALeagueOpenForRegistration(userID))
                    {
                        multiPass.SetActiveView(vwLoggedIn);
                    }
                    else
                    {
                        this.Visible = false;
                    }
                }
            }
        }

        /// <summary>
        /// Check to see if there is a league open for registration.
        /// </summary>
        /// <returns></returns>
        private bool IsALeagueOpenForRegistration()
        {
            PredicateExpression filter = new PredicateExpression();         
            filter.Add(PredicateFactory.CompareValue(
                LeagueFieldIndex.RegistrationOpen,
                ComparisonOperator.Equal,
                true));
            filter.AddWithAnd(PredicateFactory.CompareValue(
                LeagueFieldIndex.Active,
                ComparisonOperator.Equal,
                true));

            LeagueCollection leagues = new LeagueCollection();
            return (leagues.GetDbCount(filter) > 0);
        }
        /// <summary>
        /// Check to see if there is a league open for registration that the user hasn't joined yet.
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        private bool IsALeagueOpenForRegistration(Guid userID)
        {
            int[] joinedLeagues = GetUsersCurrentlyJoinedLeagues(userID);

            PredicateExpression filter = new PredicateExpression();
            if (joinedLeagues.Length > 0)
            {
                filter.Add(PredicateFactory.CompareRange(
                    LeagueFieldIndex.LeagueID,
                    true,
                    joinedLeagues));
            }
            filter.AddWithAnd(PredicateFactory.CompareValue(
                LeagueFieldIndex.RegistrationOpen,
                ComparisonOperator.Equal,
                true));
            filter.AddWithAnd(PredicateFactory.CompareValue(
                LeagueFieldIndex.Active,
                ComparisonOperator.Equal,
                true));

            LeagueCollection leagues = new LeagueCollection();
            return (leagues.GetDbCount(filter) > 0);
        }

        /// <summary>
        /// Gets the ids of the Leagues the current user is already a part of.
        /// </summary>
        /// <returns></returns>
        private int[] GetUsersCurrentlyJoinedLeagues(Guid userID)
        {
            PredicateExpression filter = new PredicateExpression();
            filter.Add(PredicateFactory.CompareValue(TeamPlayerFieldIndex.UserID, ComparisonOperator.Equal, userID));
            IRelationCollection relations = new RelationCollection();
            relations.Add(TeamEntity.Relations.TeamPlayerEntityUsingTeamID);

            TeamCollection teams = new TeamCollection();
            teams.GetMulti(filter, relations);

            int[] ids = new int[teams.Count];

            for (int i = 0; i < teams.Count; i++)
            {
                ids[i] = teams[i].LeagueID;
            }

            return ids;
        }
    }
}